package com.gome.TestUtil.httpclient;

import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;

public class SimpleHttpClient {

	public static void main(String[] args) throws HttpException, IOException {
		HttpClient client = new HttpClient();
		client.getHostConfiguration().setHost("haoma.imobile.com.cn", 80, "http");
		HttpMethod method = getPostMethod(); // 使用POST方式提交数据
		client.executeMethod(method);
		// 打印服务器返回的状态
		System.out.println(method.getStatusLine().toString());
		// 打印结果页面
		String response = new String(method.getResponseBodyAsString().getBytes(
				"utf-8"));
		
		
		//System.out.println(method.get);
		// 打印返回的信息
		System.out.println(response);
		method.releaseConnection();
//		int statuscode = method.getStatusCode();
//		if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
//				|| (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
//				|| (statuscode == HttpStatus.SC_SEE_OTHER)
//				|| (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {// 读取新的URL地址
//			Header header = method.getResponseHeader("location");
//			if (header != null) {
//				String newuri = header.getValue();
//				System.out.println("newuri==" + newuri);
//				if ((newuri == null) || (newuri.equals("")))
//					newuri = "/";
//				GetMethod redirect = new GetMethod(newuri);
//				client.executeMethod(redirect);
//				System.out.println("Redirect:"
//						+ redirect.getStatusLine().toString());
//				redirect.releaseConnection();
//			} else {
//				System.out.println("Invalid redirect");
//			}
//
//		}
	}

	/**
	 * 使用GET方式提交数据 mob=18610012669
	 * 
	 * @return
	 */
	// private static HttpMethod getGetMethod() {
	// return new GetMethod("/simcard.php?simcard=18610012669");
	// }

	/**
	 * 使用POST方式提交数据
	 * 
	 * @return
	 */
	private static HttpMethod getPostMethod() {
		PostMethod post = new PostMethod("/index.php");
		NameValuePair simcard = new NameValuePair("mob", "18610012669");
		post.setRequestBody(new NameValuePair[] { simcard });
		return post;
	}

}